package com.flashcash.solu.utils;

import android.content.Context;
import android.util.Log;

import com.android.installreferrer.api.InstallReferrerClient;
import com.android.installreferrer.api.InstallReferrerStateListener;
import com.android.installreferrer.api.ReferrerDetails;

public class InstallReferrelHelper {
    private OnInstallReferrerListener onInstallReferrerListener;

    public OnInstallReferrerListener getOnInstallReferrerListener() {
        return onInstallReferrerListener;
    }

    public void setOnInstallReferrerListener(OnInstallReferrerListener onInstallReferrerListener) {
        this.onInstallReferrerListener = onInstallReferrerListener;
    }

    public void getReferrerAsync(Context context) {
        try {
            final InstallReferrerClient installReferrerClient = InstallReferrerClient.newBuilder(context).build();
            installReferrerClient.startConnection(new InstallReferrerStateListener() {
                @Override
                public void onInstallReferrerSetupFinished(int responseCode) {
                    switch (responseCode) {
                        case InstallReferrerClient.InstallReferrerResponse.OK:
                            // Connection established, get referrer
                            getInstallReferrerImpl(installReferrerClient);
                            break;
                        case InstallReferrerClient.InstallReferrerResponse.FEATURE_NOT_SUPPORTED:
                            // API not available on the current Play Store app
                            Log.d("InstallReferrelHelper", "FEATURE_NOT_SUPPORTED");
                            break;
                        case InstallReferrerClient.InstallReferrerResponse.SERVICE_UNAVAILABLE:
                            // Connection could not be established
                            Log.d("InstallReferrelHelper", "SERVICE_UNAVAILABLE");
                            break;
                    }
                }

                @Override
                public void onInstallReferrerServiceDisconnected() {
                    // Try to restart the connection on the next request to
                    // Google Play by calling the startConnection() method.
                }

            });
        } catch (Exception ex) {
//            LogUtil.e(ex);
            Log.e("InstallReferrelHelper", ex.toString());
        }
    }

    private void getInstallReferrerImpl(InstallReferrerClient installReferrerClient) {
        if (installReferrerClient != null) {
            try {
                ReferrerDetails response = installReferrerClient.getInstallReferrer();
                String referrer = response.getInstallReferrer();
                if (onInstallReferrerListener != null) {
                    onInstallReferrerListener.OnReferrerReceived(referrer);
                }

                installReferrerClient.endConnection();
            } catch (Exception ex) {
//                LogUtil.e(ex);
                Log.e("InstallReferrelHelper", ex.toString());
            }
        }
    }

    public interface OnInstallReferrerListener {
        void OnReferrerReceived(String referrer);
    }
}
